﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using EZCRS.HCMS.Data;

namespace EZCRS.HCMS.SqlClient
{
    public class sql_AdmPresent
    {
        private string SqlStr = "";
        #region 礼品数据列表
        public DataSet Present_List(int PageSize, int PageIndex, string StrWhere)
        {
            return ExecSql.AddParamFrKeyField("hfg_present", "*", "modidate", "prsid", PageSize, PageIndex, StrWhere);
        }
        #endregion
        #region 礼品Info数据的显示,显示一条数据
        public DataRow Present_Item(string PrsId, int GId)
        {
            try
            {
                return ExecSql.GetDataSet("select * from hfg_present where prsid=" + PrsId + " and gid=" + GId.ToString() + "").Tables[0].Rows[0];
            }
            catch
            {
                return null;
            }
        }
        #endregion
        #region 礼品的添加、修改
        /// <summary>
        /// 修改或添加礼品
        /// </summary>
        /// <returns></returns>
        public bool Present_Add(string Gid, string Pid, string Prsname, string Simg, string Bimg, string Prsunit, decimal Prsprice, string Prsprovider, string Prsscore, string Prscomment, string Startdate, string Enddate, string Prscount, string Prstype, string Prsstatus, string Indate, string Inuser, string Modidate, string Modiuser, string Prsid,string psid,int languageid)
        {
            SqlParameter[] pam = new SqlParameter[19];
            pam[0] = new SqlParameter("@gid", Gid);
            pam[1] = new SqlParameter("@pid", Pid);
            pam[2] = new SqlParameter("@prsname", Prsname);
            pam[3] = new SqlParameter("@simg", Simg);
            pam[4] = new SqlParameter("@bimg", Bimg);
            pam[5] = new SqlParameter("@prsunit", Prsunit);
            pam[6] = new SqlParameter("@prsprice", Prsprice);
            pam[7] = new SqlParameter("@prsprovider", Prsprovider);
            pam[8] = new SqlParameter("@prsscore", Prsscore);
            pam[9] = new SqlParameter("@prscomment", Prscomment);
            pam[10] = new SqlParameter("@startdate", Startdate);
            pam[11] = new SqlParameter("@enddate", Enddate);
            pam[12] = new SqlParameter("@prscount", Prscount);
            pam[13] = new SqlParameter("@prstype", Prstype);
            pam[14] = new SqlParameter("@prsstatus", Prsstatus);
            pam[15] = new SqlParameter("@indate", Indate);
            pam[16] = new SqlParameter("@inuser", Inuser);
            pam[17] = new SqlParameter("@modidate", Modidate);
            pam[18] = new SqlParameter("@modiuser", Modiuser);


            if (Prsid != "")
                SqlStr = "update hfg_present set gid=@gid,pid=@pid,prsname=@prsname,simg=@simg,bimg=@bimg,prsunit=@prsunit,prsprice=@prsprice,prsprovider=@prsprovider,prsscore=@prsscore,prscomment=@prscomment,startdate=@startdate,enddate=@enddate,prscount=@prscount,prstype=@prstype,prsstatus=@prsstatus,indate=@indate,inuser=@inuser,modidate=@modidate,modiuser=@modiuser,sid="+psid+",language="+languageid.ToString()+" where prsid=" + Prsid + "";
            else
                SqlStr = "insert into hfg_present(gid,pid,prsname,simg,bimg,prsunit,prsprice,prsprovider,prsscore,prscomment,startdate,enddate,prscount,prstype,prsstatus,indate,inuser,modidate,modiuser,sid,language) values(@gid,@pid,@prsname,@simg,@bimg,@prsunit,@prsprice,@prsprovider,@prsscore,@prscomment,@startdate,@enddate,@prscount,@prstype,@prsstatus,@indate,@inuser,@modidate,@modiuser,"+psid+","+languageid.ToString()+")";
            if (ExecSql.SqlExecNoquery(SqlStr, pam) > 0)
                return true;
            else
                return false;
        }
        #endregion
        #region 礼品的删除
        public bool Present_Del(string Prsid)
        {
            string sql = "", msg = "";
            sql = "delete from hfg_present where prsid=" + Prsid;
            if (ExecSql.SqlExecNoquery(sql) > 0)
                return true;
            else
                return false;
        }
        #endregion
        #region 礼品申请列表
        public DataSet PresentConsume_List(int PageSize, int PageIndex, string StrWhere)
        {
            return ExecSql.AddParamFrKeyField("hfg_present_consume", "*", "modidate desc", "conid", PageSize, PageIndex, StrWhere);
        }
        #endregion
        #region 礼品申请Info数据的显示,显示一条数据
        public DataRow PresentConsume_Item(string ConId, int GId)
        {
            try
            {
                return ExecSql.GetDataSet("select * from hfg_present_consume where conid=" + ConId + " and gid=" + GId.ToString() + "").Tables[0].Rows[0];
            }
            catch
            {
                return null;
            }
        }
        #endregion
        #region 礼品申请的状态的改变，以及处理信息的补充
        /// <summary>
        /// 礼品申请的状态的改变，以及处理信息的补充
        /// </summary>
        /// <returns></returns>
        public bool PresentConsume_Add(string Prstype, string Modiuser, string Modidate, string Modicomment, string Conid)
        {
            SqlParameter[] pam = new SqlParameter[4];
            pam[0] = new SqlParameter("@prstype", Prstype);
            pam[1] = new SqlParameter("@modiuser", Modiuser);
            pam[2] = new SqlParameter("@modidate", Modidate);
            pam[3] = new SqlParameter("@modicomment", Modicomment);


            if (Conid != "")
                SqlStr = "update hfg_present_consume set prstype=@prstype,modiuser=@modiuser,modidate=@modidate,modicomment=@modicomment where conid=" + Conid + "";
            if (ExecSql.SqlExecNoquery(SqlStr, pam) > 0)
                return true;
            else
                return false;
        }
        #endregion
        #region 礼品申请的删除
        public bool PresentConsume_Del(string Conid)
        {
            string sql = "", msg = "";
            sql = "delete from hfg_present_consume where conid=" + Conid;
            if (ExecSql.SqlExecNoquery(sql) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 用了事务，礼品申请处理通过,把礼品状态改为5,往系统日志表里插入记录,因为成功申请后扣积分,所以插负数的
        public bool PresentConsume_Success(string Modiuser, string Modidate, string Modicomment, string Uid, string Conid)
        {
            SqlParameter[] pam = new SqlParameter[4];
            pam[0] = new SqlParameter("@modiuser", Modiuser);
            pam[1] = new SqlParameter("@modidate", Modidate);
            pam[2] = new SqlParameter("@modicomment", Modicomment);
            pam[3] = new SqlParameter("@uid", Uid);

            if (Conid != "")
            {
                SqlStr = "update hfg_present_consume set status=5,modiuser=@modiuser,modidate=@modidate,modicomment=@modicomment where conid=" + Conid + ";";
                SqlStr = SqlStr + "update hfg_scorelog set scorestatus=0,valid=1 where conid=" + Conid + " ";

            }
            if (ExecSql.TSqlExecNoquery(SqlStr, pam) > 0)
            {
                return true;
            }
            else
                return false;
        }
        #endregion
        #region 用了事务，礼品申请处理失败,把礼品状态改为4，更新hfg_user表里和这个用户对应的积分，增加相应的积分
        public bool PresentConsume_Failure(string Modiuser, string Modidate, string Modicomment, string Uid, string Conid)
        {
            SqlParameter[] pam = new SqlParameter[4];
            pam[0] = new SqlParameter("@modiuser", Modiuser);
            pam[1] = new SqlParameter("@modidate", Modidate);
            pam[2] = new SqlParameter("@modicomment", Modicomment);
            pam[3] = new SqlParameter("@uid", Uid);


            if (Conid != "")
            {
                SqlStr = "update hfg_present_consume set status=4,modiuser=@modiuser,modidate=@modidate,modicomment=@modicomment where conid=" + Conid + ";";
                SqlStr = SqlStr + "update hfg_scorelog set valid=0 where conid=" + Conid + " ";

            }
            if (ExecSql.TSqlExecNoquery(SqlStr, pam) > 0)
            {
                return true;
            }
            else
                return false;
        }
        #endregion
        /// <summary>
        /// 重置奖品申请
        /// </summary>
        /// <param name="Modiuser"></param>
        /// <param name="Modidate"></param>
        /// <param name="Modicomment"></param>
        /// <param name="Uid"></param>
        /// <param name="Score"></param>
        /// <param name="Conid"></param>
        /// <returns></returns>
        public bool PresentConsume_Reset(string Modiuser, string Modidate, string Modicomment, string Uid, string Conid)
        {
            SqlParameter[] pam = new SqlParameter[4];
            pam[0] = new SqlParameter("@modiuser", Modiuser);
            pam[1] = new SqlParameter("@modidate", Modidate);
            pam[2] = new SqlParameter("@modicomment", Modicomment);
            pam[3] = new SqlParameter("@uid", Uid);

            if (Conid != "")
            {
                SqlStr = "update hfg_present_consume set status=0,modiuser=@modiuser,modidate=@modidate,modicomment=@modicomment where conid=" + Conid + ";";//修改礼品表
                SqlStr += "update hfg_scorelog set scorestatus=1,valid=1 where conid=" + Conid + " ";//更新积分日志
                //SqlStr = SqlStr + "update hfg_user set scores=scores - @score where uid= @uid";
            }
            if (ExecSql.TSqlExecNoquery(SqlStr, pam) > 0)
            {
                return true;
            }
            else
                return false;
        }


        public bool PresentConsume_Comment(string Modiuser, string Modidate, string Modicomment, string Conid)
        {
            SqlParameter[] pam = new SqlParameter[3];
            pam[0] = new SqlParameter("@modiuser", Modiuser);
            pam[1] = new SqlParameter("@modidate", Modidate);
            pam[2] = new SqlParameter("@modicomment", Modicomment);

            if (Conid != "")
            {
                SqlStr = "update hfg_present_consume set modiuser=@modiuser,modidate=@modidate,modicomment=@modicomment where conid=" + Conid + ";";//修改礼品表
            }
            if (ExecSql.TSqlExecNoquery(SqlStr, pam) > 0)
            {
                return true;
            }
            else
                return false;
        }
    }

    public class sql_AdmPresentSort
    {
        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="sidlist">所有分类 ,分隔</param>
        /// <returns></returns>
        public bool delsort(string sidlist)
        {
            if (ExecSql.SqlExecNoquery("delete from hfg_present_sort where sid in (" + sidlist + ")") > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 保存分类
        /// </summary>
        /// <param name="sname">分类名</param>
        /// <param name="sid">分类编号 非 0 为修改</param>
        /// <returns></returns>
        public bool savesort(string sname, int sid, int gid, int pid,int languageid)
        {
            string sqlstr = "";
            if (sid > 0)
            {
                sqlstr = "update hfg_present_sort set sname=@sname,language="+languageid.ToString()+" where sid=" + sid.ToString() + "";
            }
            else
            {
                sqlstr = "insert into hfg_present_sort(sname,gid,pid,language) values(@sname," + gid.ToString() + "," + pid.ToString() + ","+languageid.ToString()+") ";
            }
            SqlParameter[] pam = new SqlParameter[1];
            pam[0] = new SqlParameter("@sname", sname);

            if (ExecSql.SqlExecNoquery(sqlstr, pam) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 保存类别排序
        /// </summary>
        /// <param name="sidlist"></param>
        /// <returns></returns>
        public bool saverank(string sidlist)
        {
            string sqlstr = "";
            string[] str = sidlist.Split(',');
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] != "")
                {
                    if (sqlstr == "")
                        sqlstr = "update hfg_present_sort set rank=" + i.ToString() + " where sid=" + str[i] + "";
                    else
                        sqlstr += ";update hfg_present_sort set rank=" + i.ToString() + " where sid=" + str[i] + "";
                }
            }
            if (ExecSql.SqlExecNoquery(sqlstr) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 返回所有类别数据
        /// </summary>
        /// <param name="gid"></param>
        /// <param name="pid"></param>
        /// <returns></returns>
        public DataTable allsort(int gid, int pid,int languageid)
        {
            return ExecSql.GetDataSet("select * from hfg_present_sort where gid=" + gid.ToString() + " and pid=" + pid.ToString() + " and language="+languageid.ToString()+" order by rank ").Tables[0];
        }

        public object allsort(int gid,int languageid)
        {
            return ExecSql.GetDataSet("select * from hfg_present_sort where gid=" + gid.ToString() + " and language="+languageid.ToString()+" order by rank ").Tables[0];
        }
    }
}
